package com.example.simple.work.controller;

import com.example.simple.work.entity.EmployeeMessage;
import com.example.simple.work.service.IEmployeeService;
import com.example.simple.work.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("employee")
public class EmployeeController extends BaseController{
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 插入员工信息
     * */
    @RequestMapping("insert")
    @CrossOrigin
    @ResponseBody
    public JsonResult<Void> insert (@RequestBody EmployeeMessage employeeMessage){
        System.out.println(employeeMessage);
        employeeService.insert(employeeMessage);
        return new JsonResult<>(OK);
    }

    /**
     * 删除员工信息
     * */
    @RequestMapping("delete")
    @CrossOrigin
    @ResponseBody
    public JsonResult<Void> delete (@RequestBody String employeeId){
        System.out.println(employeeId);
        employeeService.delete(employeeId);
        return new JsonResult<>(OK);
    }

    /** 查询所有员工信息 */
    @RequestMapping("list")
    @CrossOrigin
    public JsonResult<List<EmployeeMessage>> list (){
        List<EmployeeMessage> list = employeeService.findAllEmployeeMessageData();
        return new JsonResult<>(200,list);
    }

    /** 根据员工工号查询员工信息--重构 > 根据员工姓名模糊查询员工信息 */
    @RequestMapping("find")
    @CrossOrigin
    @ResponseBody
    public JsonResult<List<EmployeeMessage>> findMessageByEmployeeName (@RequestBody String employeeName){
        List<EmployeeMessage> list = employeeService.findMessageByEmployeeName(employeeName);
        return new JsonResult<>(200,list);
    }

    /**
     * 修改员工信息
     * */
    @RequestMapping("change")
    @CrossOrigin
    @ResponseBody
    public JsonResult<Void> change (@RequestBody EmployeeMessage employeeMessage){
        String employeeId = employeeMessage.getEmployeeId();
        employeeService.changeEmployee(employeeId,employeeMessage);
        return new JsonResult<>(OK);
    }

    /**
     * 批量插入员工信息
     * */
    @RequestMapping("insertList")
    @CrossOrigin
    @ResponseBody
    public JsonResult<Void> insertList (@RequestBody List<EmployeeMessage> employeeMessageList){
        System.out.println(employeeMessageList);
        employeeService.insertList(employeeMessageList);
        return new JsonResult<>(OK);
    }


}
